<template>
  <div
    v-loading="dataListLoading"
    class="app-container"
    :element-loading-text="loadingText"
  >
    <div class="inner-container">
      <el-form ref="queryForm" :model="queryForm" class="filter-container">
        <el-form-item class="filter-item" prop="searchKey">
          <el-input
            v-model="queryForm.searchKey"
            placeholder="请输入待办事项"
            clearable
            @clear="handleSearch"
            @keyup.enter.native="handleSearch"
          ></el-input>
        </el-form-item>
        <el-form-item class="filter-item">
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
        <el-form-item class="filter-item">
          <el-button
            v-if="isAuth('todo:add')"
            type="primary"
            @click="addOrUpdate()"
            >新增</el-button
          >
        </el-form-item>
      </el-form>

      <div class="table-box">
        <el-table :data="dataList">
          <el-table-column
            prop="name"
            label="待办事项"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="remark"
            label="描述"
            align="center"
            show-overflow-tooltip
          >
          </el-table-column>

          <el-table-column
            prop="status"
            label="是否完成"
            align="center"
            width="100"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 1">是</el-tag>
              <el-tag v-else type="danger">否</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-button
                v-if="isAuth('todo:update')"
                type="default"
                icon="el-icon-edit"
                @click="addOrUpdate(scope.row.id)"
              ></el-button>
              <el-button
                v-if="isAuth('todo:delete')"
                type="default"
                icon="el-icon-delete"
                @click="handleDelete(scope.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="page-container">
        <el-pagination
          :current-page="current"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="size"
          :total="totalCount"
          background
          layout="total, prev, pager, next,sizes,jumper"
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
        >
        </el-pagination>
      </div>
    </div>
    <todo-form
      v-if="todoFormVisible"
      ref="todoForm"
      @refreshDataList="refreshDataList"
    ></todo-form>
  </div>
</template>

<script>
import { getList, delTodo } from "@/api/todo";
import TodoForm from "./TodoForm";
import { $gc } from "gc-starter-ui-plus";

export default {
  name: "Todo",
  components: { TodoForm },
  /**
   * 继承分页
   * 继承之后，当前组件中就有了 pageMixins 中定义的属性和方法，你可以直接 this.xxx 去使用
   * 不信你看 sizeChangeHandle 和 currentChangeHandle 方法你定义了吗，那是因为 pageMixins 定义了
   */
  mixins: [$gc.mixins.pageMixins],
  data() {
    return {
      dataListLoading: false,
      loadingText: "",
      todoFormVisible: false,
      // 表单查询
      queryForm: {
        searchKey: "",
      },
      // 表格数据
      dataList: [],
    };
  },
  // 当该组件被重新加载时会回调，你可以在这里写打开这个页面后需要做的事情
  mounted() {
    this.getDataList();
  },
  methods: {
    // 查询事件
    handleSearch() {
      // 一定要加上这个，否则会出现查询不到结果的情况
      this.current = 1;
      this.getDataList();
    },
    getDataList() {
      this.dataListLoading = true;
      this.loadingText = "正在查询数据...";
      getList({
        current: this.current,
        size: this.size,
        searchKey: this.queryForm.searchKey || "",
      })
        .then((data) => {
          this.dataListLoading = false;
          this.dataList = data.list;
          this.totalCount = data.totalCount;
        })
        .catch((e) => {
          this.dataListLoading = false;
        });
    },
    // 新增或修改
    addOrUpdate(id) {
      this.todoFormVisible = true;
      this.$nextTick(() => {
        this.$refs["todoForm"].init(id);
      });
    },
    // 刷新
    refreshDataList() {
      this.queryForm.searchKey = "";
      this.handleSearch();
    },
    // 删除事件和方法
    handleDelete(id) {
      this.$confirm(`确定进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delTodo(id)
            .then((data) => {
              this.$message.success("删除成功");
              this.getDataList();
            })
            .catch((e) => {
              this.$message.success(e.msg);
            });
        })
        .catch(() => {});
    },
  },
};
</script>
